package com.example.juc.big_talk_concurrent.chapter4.article5;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test {
    // 创建 Lock 对象
    Lock lock = new ReentrantLock();
    // 通过 Lock 对象创建 Condition 对象
    Condition condition = lock.newCondition();

    public void conditionWait() throws InterruptedException {
        // 加锁
        lock.lock();
        try {
            // 挂起线程
            condition.await();
        } finally {
            // 解锁
            lock.unlock();
        }
    }

    public void conditionSignal() throws InterruptedException {
        lock.lock();
        try {
            // 唤醒线程
            condition.signal();
        } finally {
            lock.unlock();
        }
    }
}
